/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tordroid.core;

import android.util.Log;
import de.tordroid.R;
import de.tordroid.model.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Christian Simon
 */
public class Engine {

    private static String className = "core.engine";
    private static Profile PROFILE;
    private static de.tordroid.model.Character CURRENT_CHAR;
    public static String CURRENT_TAB;
    public static String CURRENT_FRAGMENT;
    private static Story CURRENT_STORY;
    public final static String FRAG_TAG = "Content Fragment";
    public final static int FRAG_ID = R.id.content;
    public final static String TAB_OVERVIEW = "de.tordroid.ui.OverviewFragment";
    public final static String TAB_CHARACTER = "de.tordroid.ui.CharacterFragment";
    public final static String TAB_STORYBOARD = "de.tordroid.ui.StoryboardFragment";

    public static void init() {
        Log.i(className + ".init()", "initializing engine");
        Database.openDatabase();

        Log.d(className + ".init()", "initializing default values");
        // Preload first Character on list
        CURRENT_CHAR = PROFILE.getCharacters().get(0);
        // Preload first Story on list
        CURRENT_STORY = CURRENT_CHAR.getStories().get(0);
    }

    public static void setProfile(Profile profile) {
        Log.d(className + ".setProfile", "Setting profile to: " + profile.getTitel());
        PROFILE = profile;
    }

    public static Profile getProfile() {
        Log.d(className + ".getProfile", "Profile: " + PROFILE.getTitel());
        return PROFILE;
    }

    public static Profile initProfile() {
        // Generate new Profile with empty data
        Log.d(className + ".initProfile", "initializing new profile");
        PROFILE = new Profile();
        CURRENT_CHAR = new de.tordroid.model.Character("New Character", false);
        CURRENT_CHAR.setStories(new ArrayList<Story>());
        CURRENT_CHAR.addStory(new Story());
        CURRENT_STORY = CURRENT_CHAR.getStories().get(0);
        PROFILE.addCharacter(CURRENT_CHAR);
        return PROFILE;
    }

    public static de.tordroid.model.Character getCurrentCharacter() {
        Log.d(className + ".getCurrentCharacter", CURRENT_CHAR.getSurname());
        return CURRENT_CHAR;
    }

    public static void setCurrentCharacter(de.tordroid.model.Character character) {
        CURRENT_CHAR = character;
        Log.d(className + ".setCurrentCharacter", CURRENT_CHAR.getSurname());
    }

    public static String getCharacterFullname() {
        Log.d(className + ".getCharacterFullname", getCharacterSurname() + " " + getCharacterLastname());
        return getCharacterSurname() + " " + getCharacterLastname();
    }

    public static List<de.tordroid.model.Character> getCharacters() {
        Log.d(className + ".getCharacters", "Profile has " + PROFILE.getCharacters().size() + " entries");
        return PROFILE.getCharacters();
    }

    public static void removeCharacter(de.tordroid.model.Character character) {
        Log.i(className + ".removeCharacter", character.getSurname());
        PROFILE.removeCharacter(character);
    }

    public static List<IconEntry> getRaces() {
        List<IconEntry> races = new ArrayList<IconEntry>();
        Log.d(className + ".getRaces", "generating List of all known races: 18/05/12");
        races.add(new IconEntry(R.string.race_Chiss, Race.Chiss.toString(), R.drawable.ic_race_chiss));
        races.add(new IconEntry(R.string.race_Cyborg, Race.Cyborg.toString(), R.drawable.ic_race_cyborg));
        races.add(new IconEntry(R.string.race_Human, Race.Human.toString(), R.drawable.ic_race_human));
        races.add(new IconEntry(R.string.race_Miraluka, Race.Miraluka.toString(), R.drawable.ic_race_miraluka));
        races.add(new IconEntry(R.string.race_Mirialan, Race.Mirialan.toString(), R.drawable.ic_race_mirialan));
        races.add(new IconEntry(R.string.race_Rattataki, Race.Rattataki.toString(), R.drawable.ic_race_rattataki));
        races.add(new IconEntry(R.string.race_Sith, Race.Sith.toString(), R.drawable.ic_race_sith));
        races.add(new IconEntry(R.string.race_Twilek, Race.Twilek.toString(), R.drawable.ic_race_twilek));
        races.add(new IconEntry(R.string.race_Zabrak, Race.Zabrak.toString(), R.drawable.ic_race_zabrak));
        return races;
    }

    public static List<IconEntry> getClasses(Faction faction) {
        List<IconEntry> classes = new ArrayList<IconEntry>();
        Log.d(className + ".getClasses", "generating List of all classes for faction: " + faction);
        switch (faction) {
            case Empire:
                classes.add(new IconEntry(R.string.class_agent, CharacterClass.Classes.Agent.toString(), R.drawable.ic_class_agent));
                classes.add(new IconEntry(R.string.class_bountyhunter, CharacterClass.Classes.Bountyhunter.toString(), R.drawable.ic_class_bountyhunter));
                classes.add(new IconEntry(R.string.class_inquisitor, CharacterClass.Classes.Inquisitor.toString(), R.drawable.ic_class_inquisitor));
                classes.add(new IconEntry(R.string.class_warrior, CharacterClass.Classes.Warrior.toString(), R.drawable.ic_class_warrior));
                break;
            case Republic:
                classes.add(new IconEntry(R.string.class_consular, CharacterClass.Classes.Consular.toString(), R.drawable.ic_class_consular));
                classes.add(new IconEntry(R.string.class_knight, CharacterClass.Classes.Knight.toString(), R.drawable.ic_class_knight));
                classes.add(new IconEntry(R.string.class_smuggler, CharacterClass.Classes.Smuggler.toString(), R.drawable.ic_class_smuggler));
                classes.add(new IconEntry(R.string.class_soldier, CharacterClass.Classes.Soldier.toString(), R.drawable.ic_class_soldier));
                break;
        }
        return classes;
    }

    public static List<IconEntry> getFactions() {
        Log.d(className + ".getFactions", "generating Empire & Republic");
        List<IconEntry> factions = new ArrayList<IconEntry>();
        factions.add(new IconEntry(R.string.faction_republic, de.tordroid.model.Faction.Republic.toString(), R.drawable.ic_faction_republic));
        factions.add(new IconEntry(R.string.faction_empire, de.tordroid.model.Faction.Empire.toString(), R.drawable.ic_faction_empire));
        return factions;
    }

    public static void setCharacterRace(String data) {
        CURRENT_CHAR.setRace(data);
    }

    public static String getCharacterRace() {
        return CURRENT_CHAR.getRace();
    }

    public static String getCharacterClass() {
        return CURRENT_CHAR.getCharacterClass().getTitel();
    }

    public static void setCharacterClass(String data) {
        CURRENT_CHAR.setCharacterClass(new CharacterClass(data));
    }

    public static Faction getCharacterFaction() {
        return CURRENT_CHAR.getFaction();
    }

    public static void setCharacterFaction(Faction faction) {
        CURRENT_CHAR.setFaction(faction);
    }

    public static int getRaceIcon() {
        List<IconEntry> entries = getRaces();
        int resId = R.drawable.ic_cancel;
        for (IconEntry e : entries) {
            if (e.getData().equals(CURRENT_CHAR.getRace())) {
                resId = e.getIcon();
            }
        }
        return resId;
    }

    public static int getFactionIcon() {
        List<IconEntry> entries = getFactions();
        int resId = R.drawable.ic_cancel;
        if(CURRENT_CHAR.getFaction().equals(Faction.Empire))
            resId = R.drawable.ic_faction_empire;
        if(CURRENT_CHAR.getFaction().equals(Faction.Republic))
            resId = R.drawable.ic_faction_republic;
        return resId;
    }

    public static int getClassIcon() {
        List<IconEntry> entries = getClasses(CURRENT_CHAR.getFaction());
        int resId = R.drawable.ic_cancel;
        for (IconEntry e : entries) {
            if (e.getData().equals(CURRENT_CHAR.getCharacterClass().getTitel())) {
                resId = e.getIcon();
            }
        }
        return resId;
    }

    public static int getAvailabilityIcon(de.tordroid.model.Character character) {
        if (character.isAvailable()) {
            return R.drawable.ic_visible;
        } else {
            return R.drawable.ic_invisible;
        }
    }

    public static void setCharacterSurname(String titel) {
        CURRENT_CHAR.setSurname(titel);
    }

    public static void setCharacterLastname(String legacy) {
        CURRENT_CHAR.setLastname(legacy);
    }

    public static List<String> getCharacterStoryList() {
        List<String> stories = new ArrayList<String>();
        for (Story story : CURRENT_CHAR.getStories()) {
            stories.add(story.getTitel());
        }
        return stories;
    }

    public static List<Story> getCharacterStories() {
        List<Story> stories = new ArrayList<Story>();
        for(Story story : CURRENT_CHAR.getStories())
        {
            if(!story.isSubStory())
                stories.add(story);
        }
        return stories;
    }

    public static boolean checkNullValue(String toString) {
        if (toString.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public static void setCharacterAvailablility(boolean checked) {
        CURRENT_CHAR.setAvailablility(checked);
    }

    public static String getCharacterBackground() {
        return CURRENT_CHAR.getBackground();
    }

    public static void setCharacterBackground(String obj) {
        CURRENT_CHAR.setBackground(obj);
    }

    public static void setCharacterAge(int i) {
        CURRENT_CHAR.setAge(i);
    }

    public static int getCharacterAge() {
        return CURRENT_CHAR.getAge();
    }

    public static void addCharacterStory(Story story) {
        CURRENT_CHAR.addStory(story);
    }

    public static Story getCharacterStory(String titel) {
        for (Story story : CURRENT_CHAR.getStories()) {
            if (story.getTitel().equals(titel)) {
                return story;
            }
        }
        return null;
    }

    public static void setStoryTitel(String toString) {
        CURRENT_STORY.setTitel(toString);
    }

    public static void setStoryContent(String toString) {
        CURRENT_STORY.setStory(toString);
    }

    public static void delCharacterStory(Story story) {
        CURRENT_CHAR.removeStory(story);
    }

    public static List<String> getCharacterStoryTitel() {
        List<String> list = new ArrayList<String>();
        for (Story story : CURRENT_CHAR.getStories()) {
            list.add(story.getTitel());
        }
        return list;
    }

    public static List<String> getCharacterStoryTimeLine() {
        List<String> list = new ArrayList<String>();
        for (Story story : CURRENT_CHAR.getStories()) {
            list.add(String.format("{0} NVC", story.getIngameTime()));
        }
        return list;
    }

    public static List<de.tordroid.model.Character> getDummyChars() {
        List<de.tordroid.model.Character> list = new ArrayList<de.tordroid.model.Character>();
        list.add(new de.tordroid.model.Character());

        return list;
    }

    public static void removeStory() {
        if (Engine.getCharacterStories().contains(Engine.getCurrentStory())) {
            CURRENT_CHAR.removeStory(CURRENT_STORY);
        }
    }

    public static void setCurrentStory(Story child) {
        CURRENT_STORY = child;
    }

    public static String getStoryTitel() {
        return CURRENT_STORY.getTitel();
    }

    public static String getStoryContent() {
        return CURRENT_STORY.getStory();
    }

    public static String getStoryDate() {
        return CURRENT_STORY.getAddDate();
    }

    public static int getStoryTime() {
        return CURRENT_STORY.getIngameTime();
    }

    public static Story getCurrentStory() {
        return CURRENT_STORY;
    }

    public static String getCharacterSurname() {
        return CURRENT_CHAR.getSurname();
    }

    public static String getCharacterLastname() {
        return CURRENT_CHAR.getLastname();
    }

    public static void addCharacter() {
        de.tordroid.model.Character character = new de.tordroid.model.Character();
        Log.i(className + ".addCharacter", character.getSurname());
        character.setSurname("New Character");
        character.setLastname("");
        character.setRace(Race.None.toString());
        character.setBackground("");
        character.setAvailablility(false);
        character.setCharacterClass(new CharacterClass());
        character.setFaction(Faction.None);
        character.setStories(new ArrayList<Story>());
        PROFILE.getCharacters().add(character);
    }

    public static void setStoryTime(int value) {
        CURRENT_STORY.setIngameTime(value);
    }

    public static void setStoryIsAtc(boolean checked) {
        CURRENT_STORY.setAtc(checked);
    }
    
    public static boolean getStoryIsAtc()
    {
        return CURRENT_STORY.isAtc();
    }
}
